//
//  HomeStatusListViewModel.swift
//  BQWB
//
//  Created by laibingqiang on 16/10/26.
//  Copyright © 2016年 laibingqiang. All rights reserved.
//

import UIKit
import SDWebImage
class HomeStatusListViewModel: NSObject {
    
    var homeDataArr:[HomeStatusViewModel] = [HomeStatusViewModel]()
    
    func getHomeDataInfo(isAnimating:Bool,callBack:@escaping (Bool,String)->()){
        var sinceID:Int64 = 0
        var maxID:Int64 = 0
        if isAnimating == true{
            maxID = homeDataArr.last?.statusModel?.id ?? 0
            if maxID > 0{
                maxID -= 1
            }
            sinceID = 0
        }
        else
        {
            sinceID = homeDataArr.first?.statusModel?.id ?? 0
            maxID = 0
        }
        
        NetworkTool.shared.getHomeData(sinceID: sinceID, maxID: maxID, success: { (response) in
            guard let res = response as? [String:Any] else{
                callBack(false,"请求数据失败")
                return
            }
            
            guard let Arr = res["statuses"] as? [[String:Any]] else{
                callBack(false,"请求数据失败")
                return
            }
            
            let arr = NSArray.yy_modelArray(with: HomeStatusModel.self, json: Arr) as! [HomeStatusModel]?
            
            
            
            var arrM = [HomeStatusViewModel]()
            for model in arr!{
                
                let statusModel = HomeStatusViewModel()
                
                statusModel.statusModel = model
                arrM.append(statusModel)
            }
            //self.homeDataArr = arrM
            if isAnimating{
                self.homeDataArr = self.homeDataArr + arrM
            }
            else
            {
                self.homeDataArr = arrM + self.homeDataArr

            }
            
            self.loadSingleIma(arr: arrM, callBack: callBack)
//            callBack(true)
            
            }) { (error) in
                
                callBack(false,"请求数据失败")
                
        }
       
    }


}
extension HomeStatusListViewModel{
    
    func loadSingleIma(arr:[HomeStatusViewModel],callBack:@escaping (Bool,String)->()){
        
        let group = DispatchGroup()
        for model in arr{
            
            if model.statusModel?.pic_urls?.count == 1{
                
                let url = URL(string: (model.statusModel?.pic_urls?.last?.thumbnail_pic)!)
                group.enter()
                
                SDWebImageManager.shared().downloadImage(with: url, options: [], progress: nil, completed: { (img, error, _, _, _) in
                    
                    group.leave()
                })
                
            }
            
            if model.statusModel?.retweeted_status?.pic_urls?.count == 1{
                let url = URL(string: (model.statusModel?.retweeted_status?.pic_urls?.last?.thumbnail_pic)!)
                group.enter()
                
                SDWebImageManager.shared().downloadImage(with: url, options: [], progress: nil, completed: { (img, error, _, _, _) in
                    
                    group.leave()
                })
            }
            
           
        }
        group.notify(queue: DispatchQueue.main, execute: {
            
            let str:String = "更新了\(arr.count)条消息"
            callBack(true,str)
        })
        
    }
}
